// const baseURL = "http://192.168.50.34:8901"; // 
const baseURL = 'http://223.84.153.141:8901'
const request = ({ url, data, method }) => {
  let header = { "Content-Type": "application/json" };
  // header["Authorization"] = 'Bearer eyJhbGciOiJIUzUxMiJ9.eyJsb2dpbl91c2VyX2tleSI6ImU4YmI0M2QyLTZlZjktNDJiMy1iM2U4LWVjNjZiY2I5YTdlNiJ9.lezUMiMxp1iJgt79t25giBLZUkSXZCUesiAWYBZoYaB1Z_48ElnxWqdRAHn78IKVhtr1gnyHuJG0Cf9rrTUrZA' //uni.getStorageSync("token") || ""; // 让每个请求携带自定义token 请根据实际情况自行修改

  if (uni.getStorageSync("token")) {
    header["Authorization"] = uni.getStorageSync("token") || ""; // 让每个请求携带自定义token 请根据实际情况自行修改
  }
  return new Promise((resolve, reject) => {
    uni.request({
      url: baseURL + url,
      method: method || "POST",
      data,
      header,
      success: (res) => {
        const code = res.data.code;
        if(code==200) {
          resolve(res.data);
        } else if (code == 401) {
          //退出登录
          console.log('登录失效');
          uni.showToast({
            title: "登录失效",
            icon: "none",
            duration: 2000,
          });
          uni.reLaunch({
            url: "/pages/login",
          });
          reject("error");
        } else {
          uni.showToast({
            title: res.error,
            icon: "none",
            duration: 2000,
          });
          reject("error");
        }
      },
      fail: (err) => {
        uni.showToast({
          title: err.error || '',
          icon: "none",
          duration: 2000,
        });
        reject(err);
      },
      complete: (err)=>{
        if(err.data.code!==200) {
          uni.showToast({
            title: err.data.error || err.data.msg || '请求失败',
            icon: "none",
            duration: 2000,
          });
        }
      }
    });
  });
};

export default request;
